/**
 * 
 */
package com.nari.sink.action;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.service.IFileMappingService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.sink.bo.ISinkerStandardDict;
import com.nari.sink.service.ISinkerStandardDictService;
import com.nari.sink.utils.excelupload.res.SinkerMgrExcelResource;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2014-4-15
 */
public class SinkerStandardDictAction extends JSONActionSupport {
	private static final long serialVersionUID = -5068180879995976297L;
	private ISinkerStandardDictService iSinkerStandardDictService;
	private IFileMappingService iFileMappingService;
	//标准散热器信息查询
	private String dictSerious;
	private String sinkType;
	private String brand;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<ISinkerStandardDict> sinkerDictPage;
	//标准散热器信息管理
	private String sinkerDictUID;
	private ISinkerStandardDict sinkerDict;
	//散热器型号照片附件
	private IFileMapping attachFileMap;
	private File attachFile;
	private String attachFileFileName;
	//散热器型号照片页面展现路径
	private String sinkerDictPicRelFolder; 
	private String sinkerDictPicURL;
	
	/**
	 * 分页查询标准散热器库
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-15
	 */
	public String queryStandardSinkerPage() throws Exception {
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.sinkerDictPage = this.iSinkerStandardDictService.querySinkerDictPage(dictSerious, sinkType, brand, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.sinkerDictPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.sinkerDictPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "standardSinkerPage";
	}
	
	/**
	 * 标准散热器信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-18
	 */
	public String querySinkerDictForManage() throws Exception {
		if(this.sinkerDictUID!=null && !"".equals(sinkerDictUID)) {
			this.sinkerDict = this.iSinkerStandardDictService.queryEntityById(sinkerDictUID);
			//散热器照片附件
			if(sinkerDict.getPictureNo()!=null && !"".equals(sinkerDict.getPictureNo().trim())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(sinkerDict.getPictureNo());
			}
		}
		return "sinkerDictManage";
	}
	
	/**
	 * 新增或修改标准散热器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-18
	 */
	public String saveOrUpdateSinkerDict() throws Exception {
		this.rmsg = iSinkerStandardDictService.saveOrUpdateSinkerDict(sinkerDict, attachFile, attachFileFileName);
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 异步删除标准散热器附件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-18
	 */
	public String deleteSinkerDictAttach() throws Exception {
		this.rmsg = iSinkerStandardDictService.deleteSinkerDictAttachFile(sinkerDictUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据标准散热器唯一标识删除型号数据
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-18
	 */
	public String deleteSinkerDictByUID() throws Exception {
		this.rmsg = iSinkerStandardDictService.deleteSinkerDictByUID(sinkerDictUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 标准散热器图片附件上传初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-19
	 */
	public String initSinkerDictImageUpload() throws Exception {
		//携带参数：散热器型号 - sinkerDictUID
		return "sinkerImageUploadInit";
	}
	
	/**
	 * 标准散热器照片上传
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-19
	 */
	public String saveSinkerDictImageUpload() throws Exception {
		this.rmsg = iSinkerStandardDictService.saveSinkerDictImageUpload(attachFile, attachFileFileName, sinkerDictUID);
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 散热器库批量导入初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-18
	 */
	public String initSinkerDictUpload() throws Exception {
		//可携带参数
		return "sinkerDictUploadInit";
	}
	
	/**
	 * 标准散热器型号数据的批量导入
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-17
	 */
	public String uploadSinkerDictBatch() throws Exception {
		if(attachFileFileName.toLowerCase().endsWith("xls") 
				|| attachFileFileName.toLowerCase().endsWith("xlsx")) {
			this.rmsg = iSinkerStandardDictService.saveOrUpdateSinkerDictBatch(attachFile, attachFileFileName);
		} else {
			this.rmsg = ResponseMsg.getInstance(false, "请上传正确的Excel数据文件！");
		}
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 下载Excel导入模板
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public String downloadExcelTemplate() throws Exception {
		//设置文件输出流
		this.setTargetFile(SinkerMgrExcelResource.getSinkerTemplateResource());
		//设置模板名称
		this.setOutputFileName("标准散热器库Excel导入模板.xls");
		return "downloadTemplate";
	}
	
	/**
	 * 查询散热器型号页面展现路径
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-19
	 */
	public String querySinkerDictPicRelURL() throws Exception {
		this.sinkerDict = iSinkerStandardDictService.queryEntityById(sinkerDictUID);
		this.sinkerDictPicURL = "";
		if(sinkerDict != null) {
			IFileMapping fileMapping = iFileMappingService.queryEntityById(sinkerDict.getPictureNo());
			if(fileMapping != null) {
				//构造散热器型号照片的展现路径
				sinkerDictPicURL = this.sinkerDictPicRelFolder + "/" +fileMapping.getSavePath();
				this.rmsg = ResponseMsg.getInstance(true, sinkerDictPicURL);
			} else {
				this.rmsg = ResponseMsg.getInstance(false, "图片已被删除，无法加载！");
			}
		} else {
			this.rmsg = ResponseMsg.getInstance(false, "型号散热器已被删除，无法加载图片！");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询散热器型号详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-21
	 */
	public String querySinkerdictDetail() throws Exception {
		this.sinkerDict = iSinkerStandardDictService.queryEntityById(sinkerDictUID);
		this.sinkerDictPicURL = "";
		if(sinkerDict != null) {
			IFileMapping fileMapping = iFileMappingService.queryEntityById(sinkerDict.getPictureNo());
			if(fileMapping != null) {
				//构造散热器型号照片的展现路径
				sinkerDictPicURL = this.sinkerDictPicRelFolder + "/" +fileMapping.getSavePath();
			}
		}
		return "sinkerDictDetail";
	}

	/**
	 * @return the iSinkerStandardDictService
	 */
	@JSON(serialize=false)
	public ISinkerStandardDictService getiSinkerStandardDictService() {
		return iSinkerStandardDictService;
	}

	/**
	 * @param iSinkerStandardDictService the iSinkerStandardDictService to set
	 */
	public void setiSinkerStandardDictService(
			ISinkerStandardDictService iSinkerStandardDictService) {
		this.iSinkerStandardDictService = iSinkerStandardDictService;
	}

	/**
	 * @return the iFileMappingService
	 */
	@JSON(serialize=false)
	public IFileMappingService getiFileMappingService() {
		return iFileMappingService;
	}

	/**
	 * @param iFileMappingService the iFileMappingService to set
	 */
	public void setiFileMappingService(IFileMappingService iFileMappingService) {
		this.iFileMappingService = iFileMappingService;
	}

	/**
	 * @return the dictSerious
	 */
	public String getDictSerious() {
		return dictSerious;
	}

	/**
	 * @param dictSerious the dictSerious to set
	 */
	public void setDictSerious(String dictSerious) {
		this.dictSerious = dictSerious;
	}

	/**
	 * @return the sinkType
	 */
	public String getSinkType() {
		return sinkType;
	}

	/**
	 * @param sinkType the sinkType to set
	 */
	public void setSinkType(String sinkType) {
		this.sinkType = sinkType;
	}

	/**
	 * @return the brand
	 */
	public String getBrand() {
		return brand;
	}

	/**
	 * @param brand the brand to set
	 */
	public void setBrand(String brand) {
		this.brand = brand;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the sinkerDictPage
	 */
	public ResultWithPageCond<ISinkerStandardDict> getSinkerDictPage() {
		return sinkerDictPage;
	}

	/**
	 * @param sinkerDictPage the sinkerDictPage to set
	 */
	public void setSinkerDictPage(
			ResultWithPageCond<ISinkerStandardDict> sinkerDictPage) {
		this.sinkerDictPage = sinkerDictPage;
	}

	/**
	 * @return the sinkerDictUID
	 */
	public String getSinkerDictUID() {
		return sinkerDictUID;
	}

	/**
	 * @param sinkerDictUID the sinkerDictUID to set
	 */
	public void setSinkerDictUID(String sinkerDictUID) {
		this.sinkerDictUID = sinkerDictUID;
	}

	/**
	 * @return the sinkerDict
	 */
	public ISinkerStandardDict getSinkerDict() {
		return sinkerDict;
	}

	/**
	 * @param sinkerDict the sinkerDict to set
	 */
	public void setSinkerDict(ISinkerStandardDict sinkerDict) {
		this.sinkerDict = sinkerDict;
	}

	/**
	 * @return the attachFileMap
	 */
	public IFileMapping getAttachFileMap() {
		return attachFileMap;
	}

	/**
	 * @param attachFileMap the attachFileMap to set
	 */
	public void setAttachFileMap(IFileMapping attachFileMap) {
		this.attachFileMap = attachFileMap;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

	/**
	 * @return the sinkerDictPicRelFolder
	 */
	public String getSinkerDictPicRelFolder() {
		return sinkerDictPicRelFolder;
	}

	/**
	 * @param sinkerDictPicRelFolder the sinkerDictPicRelFolder to set
	 */
	public void setSinkerDictPicRelFolder(String sinkerDictPicRelFolder) {
		this.sinkerDictPicRelFolder = sinkerDictPicRelFolder;
	}

	/**
	 * @return the sinkerDictPicURL
	 */
	public String getSinkerDictPicURL() {
		return sinkerDictPicURL;
	}

	/**
	 * @param sinkerDictPicURL the sinkerDictPicURL to set
	 */
	public void setSinkerDictPicURL(String sinkerDictPicURL) {
		this.sinkerDictPicURL = sinkerDictPicURL;
	}
	
}
